package com.chuangjiangx.member.score.web.controller;

import com.chuangjiangx.commons.CurrentThreadContext;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.ddd.application.MemberApplication;
import com.chuangjiangx.member.score.ddd.application.command.ExchangeGoodsCommand;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreGiftRecord;
import com.chuangjiangx.member.basic.web.controller.BaseController;
import com.chuangjiangx.member.basic.web.interceptor.Login;
import com.chuangjiangx.member.basic.web.request.CheckPhone;
import com.chuangjiangx.member.score.web.request.ConvertGiftRequest;
import com.chuangjiangx.member.score.web.response.GiftsListForH5Response;
import com.chuangjiangx.member.common.utils.BeanUtils;
import com.chuangjiangx.member.score.ddd.query.MbrScoreGiftRuleQuery;
import com.chuangjiangx.member.score.ddd.dal.condition.QueryScoreGiftRuleCondition;
import com.chuangjiangx.member.basic.ddd.query.condition.SearchMemberInfoCondition;
import com.chuangjiangx.member.score.ddd.dal.dto.MbrScoreGiftRuleDetail;
import com.chuangjiangx.member.score.ddd.dal.dto.MemberScoreGiftRuleList;
import com.chuangjiangx.member.score.ddd.query.dto.GiftsListForH5DTO;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserContext;
import com.chuangjiangx.member.score.ddd.domain.model.MbrScoreExchangeType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 积分商城
 */
@RestController
@RequestMapping(value = "/h5/shop", produces = "application/json")
public class ScoreShopController extends BaseController {

    @Autowired
    private MbrScoreGiftRuleQuery mbrScoreGiftRuleQuery;

    @Autowired
    private MemberApplication memberApplication;

    /**
     * 积分商城兑换物品列表
     *
     */
    @RequestMapping(value = "/goods-list")
    @Login
    public Response scoreShopGoodsList() {
        MbrUserContext user = (MbrUserContext) CurrentThreadContext.getCurrentUser();
        QueryScoreGiftRuleCondition condition = new QueryScoreGiftRuleCondition();
        condition.setMerchantId(user.getMerchantId());
        condition.setStatus(1);
        condition.setPageSize(50);
        PagingResult<MemberScoreGiftRuleList> list = mbrScoreGiftRuleQuery.findMbrScoreGiftRuleList(condition);
        return ResponseUtils.successCamel(list.getItems());
    }

    /**
     * 兑换物品详情
     *
     * @param id     规则id
     */
    @RequestMapping(value = "/goods-detail")
    @Login
    public Response shopGoodsDetail(Long id) {
        MbrUserContext memeberValue = (MbrUserContext) CurrentThreadContext.getCurrentUser();
        MbrScoreGiftRuleDetail detail = mbrScoreGiftRuleQuery.findByIdAndMerchantId(id, memeberValue.getMerchantId());
        return ResponseUtils.successCamel(detail);
    }

    /**
     * 会员兑换商品取货码
     *
     * @param convertGiftRequest 兑换商品请求参数
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @throws Exception
     */
    @RequestMapping(value = "/convert-gift")
    @ResponseBody
    @Login
    public Response convertGift(@Validated @RequestBody ConvertGiftRequest convertGiftRequest) throws Exception {

        MbrUserContext user = (MbrUserContext) CurrentThreadContext.getCurrentUser();

        //兑换商品所需参数
        ExchangeGoodsCommand command = new ExchangeGoodsCommand();
        command.setMerchantId(user.getMerchantId());
        command.setMerchantUserId(user.getMerchantUserId());
        command.setMemberId(convertGiftRequest.getMemberId());
        command.setScoreGiftRuleId(convertGiftRequest.getId());
        command.setScoreExchangeType(MbrScoreExchangeType.ONLINE);
        //兑换操作
        MbrScoreGiftRecord record = memberApplication.exchangeGoods(command);
        return ResponseUtils.successCamel("recordId",record.getId().getId());
    }

    /**
     * 兑换商品列表
     *
     * @param checkPhone 手机号码
     * @param request
     * @return 商品列表
     * @throws Exception
     */
    @RequestMapping(value = "/searchGoodsList")
    @ResponseBody
    @Login
    @Deprecated
    public Response searchGoodsList(@Validated @RequestBody CheckPhone checkPhone, HttpServletRequest request) throws Exception {

        MbrUserContext memeberValue = (MbrUserContext) CurrentThreadContext.getCurrentUser();

        SearchMemberInfoCondition condition = new SearchMemberInfoCondition();
        condition.setMobile(checkPhone.getMobile());
        condition.setMerchantId(memeberValue.getMerchantId());
        List<GiftsListForH5DTO> giftsListForH5DTOs = mbrScoreGiftRuleQuery.searchGoodsListForH5(condition);
        List<GiftsListForH5Response> listForH5Responses = BeanUtils.convertCollection(giftsListForH5DTOs, GiftsListForH5Response.class, null);
        return ResponseUtils.success("giftList", listForH5Responses);
    }
}
